package com.lbmb.photodrawaimanager.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author hanWang
 * @Title:
 * @Package
 * @Description:
 * @date 2023/8/33:08 上午
 */
public class PageUtils<T> implements Serializable {
    private static final long serialVersionUID = 1L;
  //  @ApiModelProperty("总记录数")
    private int totalCount;
   // @ApiModelProperty("每页记录数")
    private int pageSize;
   // @ApiModelProperty("总页数")
    private int totalPage;
   // @ApiModelProperty("当前页数")
    private int currPage;
   // @ApiModelProperty("列表数据")
    private List<T> list;

    public PageUtils(IPage<T> page) {
        this.list = page.getRecords();
        this.totalCount = (int)page.getTotal();
        this.pageSize = (int)page.getSize();
        this.currPage = (int)page.getCurrent();
        this.totalPage = (int)page.getPages();
    }

    public PageUtils(List<T> list, int totalCount, int pageSize, int currPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int)Math.ceil((double)totalCount / (double)pageSize);
    }

    public PageUtils() {
    }

    public PageUtils<T> getPages(Integer pageNum, Integer pageSize, List<T> list) {
        int pages = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
        PageUtils<T> iPage = new PageUtils();
        iPage.setTotalCount(list.size());
        iPage.setCurrPage(pageNum);
        iPage.setTotalPage(pages);
        if (pages != 0 && pages >= pageNum) {
            int beginNum = (pageNum - 1) * pageSize;
            int endNum = list.size() - beginNum > pageSize ? beginNum + pageSize : list.size();
            iPage.setList(list.subList(beginNum, endNum));
        } else {
            iPage.setList(Collections.emptyList());
        }

        return iPage;
    }

    public boolean hasNextPage() {
        return this.currPage < this.totalPage;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return this.totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrPage() {
        return this.currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public List<T> getList() {
        return this.list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
